﻿using Mate.Core.Configuration;
using Mate.Db.SqlSugar;
using Mate.Framework.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using UEditor.Core;

namespace Mate.Framework.Extensions
{
    public static class ServiceCollectionExtensions
    {

        public static IServiceProvider ConfigureApplicationServices(this IServiceCollection services, IConfiguration configuration,string adminOrApi="api")
        {
            services.ConfigureStartupConfig<DataSettings>(configuration.GetSection("DataSettings"));
            var mateConfig = services.ConfigureStartupConfig<MateConfig>(configuration.GetSection("MateConfig"));

            

            services.AddApplicationMvc();
            services.AddHttpSession();
            if (adminOrApi == "admin")
            {
                services.AddUEditorService();
                services.AddAuthentication();
            }
      

            return services.Register(configuration, mateConfig);
        }

        private static TConfig ConfigureStartupConfig<TConfig>(this IServiceCollection services, IConfiguration configuration) where TConfig : class, new()
        {
            if (services == null)
                throw new ArgumentNullException(nameof(services));

            if (configuration == null)
                throw new ArgumentNullException(nameof(configuration));

            //create instance of config
            var config = new TConfig();

            //bind it to the appropriate section of configuration
            configuration.Bind(config);

            //and register it as a service
            services.AddSingleton(config);

            return config;
        }


        private static IMvcBuilder AddApplicationMvc(this IServiceCollection services)
        {
            var mvcBuilder = services.AddMvc();
            mvcBuilder.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            //mvcBuilder.AddMvcOptions(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); });

            mvcBuilder.AddControllersAsServices();

            mvcBuilder.AddJsonOptions(options =>
            {
                options.SerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
            });

            return mvcBuilder;
        }

        private static void AddHttpSession(this IServiceCollection services)
        {
            services.AddSession(options =>
            {
                options.Cookie.Name = ".Mate.Session";
                options.Cookie.HttpOnly = true;
            });
        }

        private static void AddAuthentication(this IServiceCollection services)
        {
            #region 登录认证配置
            services.AddAuthentication(options =>
            {
                options.DefaultChallengeScheme = AuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = AuthenticationDefaults.AuthenticationScheme;
                options.DefaultAuthenticateScheme = AuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(AuthenticationDefaults.AuthenticationScheme, m =>
            {
                m.LoginPath = AuthenticationDefaults.LoginPath;
                //m.AccessDeniedPath = new PathString("/Account/Denied");
                m.LogoutPath = AuthenticationDefaults.LogoutPath;
                m.Cookie.Path = "/";
            });
            #endregion
            //var authenticationBuilder = services.AddAuthentication(options =>
            //{
            //    options.DefaultChallengeScheme = AuthenticationDefaults.AuthenticationScheme;
            //    options.DefaultSignInScheme = AuthenticationDefaults.ExternalAuthenticationScheme;
            //});

            //authenticationBuilder.AddCookie(AuthenticationDefaults.AuthenticationScheme, options =>
            //{
            //    options.Cookie.Name = AuthenticationDefaults.CookiePrefix + AuthenticationDefaults.AuthenticationScheme;
            //    options.Cookie.HttpOnly = true;
            //    options.LoginPath = AuthenticationDefaults.LoginPath;
            //    options.LogoutPath = AuthenticationDefaults.LogoutPath;
            //});



            //authenticationBuilder.AddCookie(AuthenticationDefaults.ExternalAuthenticationScheme, options =>
            //{
            //    options.Cookie.Name = AuthenticationDefaults.CookiePrefix + AuthenticationDefaults.ExternalAuthenticationScheme;
            //    options.Cookie.HttpOnly = true;
            //    options.LoginPath = AuthenticationDefaults.LoginPath;
            //    options.LogoutPath = AuthenticationDefaults.LogoutPath;
            //});
        }

    }
}
